import asyncio
import os
from pathlib import Path
from urllib.parse import urlencode
from playwright.async_api import async_playwright


async def main():
    root = Path(__file__).resolve().parents[1]
    template = root / 'luogu-snap' / 'src' / 'template.html'
    params = {
        'problem': 'P1001',
        'title': 'A + B Problem',
        'rid': '123456789',
        'lang': 'C++17',
        'time': '2025-01-01 12:00:00',
        'code': '#include <bits/stdc++.h>\nusing namespace std;\nint main(){long long a,b;cin>>a>>b;cout<<a+b;}'
    }
    url = f"file://{template.as_posix()}?{urlencode(params)}"
    out = root / 'sample_py.png'
    async with async_playwright() as pw:
        browser = await pw.chromium.launch()
        page = await browser.new_page()
        await page.goto(url)
        el = await page.query_selector('#canvas')
        await el.screenshot(path=str(out))
        await browser.close()
    print('Saved', out)


if __name__ == '__main__':
    asyncio.run(main())

